package com.alibaba.cloud.application.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import com.alibaba.cloud.application.EmailInfoService;
import com.alibaba.cloud.domain.EmailLog;
import com.alibaba.cloud.infrastructure.mapper.EmailLogMapper;

import freemarker.template.Configuration;
import freemarker.template.Template;
import jakarta.mail.internet.MimeMessage;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
@RequiredArgsConstructor
public class EmailInfoServiceImpl implements EmailInfoService {
    private final EmailLogMapper emailLogMapper;
    // 执行者
    private final JavaMailSender mailSender;
    // freemarker
    private final Configuration configuration;
    // 发送者
    @Value("${spring.mail.username}")
    public String USER_NAME;
    // 发送者
    @Value("${server.path}")
    public String PATH;

    static {
        System.setProperty("mail.mime.splitlongparameters", "false");
    }

    @Override
    public void send(EmailLog email) {
        log.info("发送邮件：{}", email);
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(USER_NAME);
            message.setTo(email.getReceive());
            message.setSubject(email.getSubject());
            message.setText(email.getContent());
            mailSender.send(message);
            // 发送记录
            emailLogMapper.insert(email);
        } catch (Exception e) {
            log.error("邮件：{}，发送失败,", email, e);
           // throw new MessageSendException(ResultCodeEnum.EMAIL_SEND_FAIL, ResultCodeEnum.EMAIL_SEND_FAIL.desc());
        }
    }

    @Override
    public void sendFreemarker(EmailLog email) {
        try {
            MimeMessage message = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            // 这里可以自定义发信名称比如：爪哇笔记
            helper.setFrom(USER_NAME, "汤姆克鲁斯");
            helper.setTo(email.getReceive());
            helper.setSubject(email.getSubject());
            Map<String, Object> model = new HashMap<>();
            model.put("mail", email);
            model.put("path", PATH);
            Template template = configuration.getTemplate(email.getTemplate());
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, model);
            helper.setText(text, true);
            mailSender.send(message);
            email.setContent(text);
            // 发送记录
            emailLogMapper.insert(email);
        } catch (Exception e) {
            log.error("邮件：{} 发送失败", email, e);
            //throw new MessageSendException(ResultCodeEnum.EMAIL_SEND_FAIL, ResultCodeEnum.EMAIL_SEND_FAIL.desc());
        }
    }
}
